package com.app.shop.activitys;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.app.library.base.BaseActivity;
import com.app.library.beans.UserBean;
import com.app.library.utils.SharedPreferencesUtil;
import com.app.library.utils.StringUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.shop.R;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PassWordResetActivity extends BaseActivity {


    private TextView tvTitle;
    private EditText inputOld;
    private EditText inputpwdNew;
    private EditText inputpwdRepeat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_word_reset);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        inputOld= (EditText) findViewById(R.id.inputpwd_old);
        inputpwdNew= (EditText) findViewById(R.id.inputpwd_new);
        inputpwdRepeat= (EditText) findViewById(R.id.inputpwd_repeat);
        ImageView imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        TextView   tvmodify= (TextView) findViewById(R.id.tv_modify);
        imgvreturn.setOnClickListener(this::onViewClicked);
        tvmodify.setOnClickListener(this::onViewClicked);
        tvTitle.setText("重置密码");
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.tv_modify:

                String old = inputOld.getText().toString();
                String pwd = inputpwdNew.getText().toString();
                String pwd_2 = inputpwdRepeat.getText().toString();

                if (StringUtil.isEmpty(old) || StringUtil.isEmpty(pwd) || StringUtil.isEmpty(pwd_2)) {
                    ToastUtil.showToast(this, "请完善信息");
                    return;
                }

                if (!pwd.equals(pwd_2)) {
                    ToastUtil.showToast(this, "两次密码不一致");
                    return;
                }

                UserBean userBean = UserManager.getUser(UserManager.getUserId(this));
                if (!old.equals(userBean.password)) {
                    ToastUtil.showToast(this, "旧密码验证错误，请重新输入");
                    return;
                }


                ContentValues values = new ContentValues();
                values.put("password", pwd);
                SharedPreferencesUtil.saveData(this, "user", "password", pwd);
                DataSupport.updateAll(UserBean.class, values, "user_id=?", userBean.user_id);

                ToastUtil.showToast(this, "修改成功");

                onBackPressed();

                break;
        }
    }
}
